<template>
  <div class="remark" v-loading="remarkListLoading">
    <div class="remark-content" v-if="remarkList.length > 0">
      <div class="title">
        评论 <span>{{ total }}</span>
      </div>
      <div
        class="remark-item"
        v-for="(item, index) in remarkList"
        :key="item.id"
      >
        <div class="avatar">
          <el-image
            :src="item.avatar || defaultAvatar"
            style="border-radius: inherit"
          ></el-image>
        </div>
        <div class="item-info">
          <div class="info-header">
            <div class="nickname">{{ item.userName }}</div>
            <div class="create-time">{{ formatDate(item.createTime) }}</div>
          </div>
          <span
            :class="{ content: !isShow[index] || !isFourLines(item.content) }"
            v-html="item.content"
          ></span>
          <span
            style="font-size: 15px; color: #aaa; cursor: pointer"
            @click="toggleShow(index)"
          >
            {{
              isFourLines(item.content)
                ? isShow[index]
                  ? "[收起]"
                  : "[展开]"
                : ""
            }}
          </span>
        </div>
      </div>
    </div>
    <div class="remark-empty" v-else>
      <el-empty description="暂无评论"></el-empty>
    </div>
  </div>
</template>

<script>
import defaultAvatar from "@/assets/default.jpg";
export default {
  name: "RemarkView",
  data() {
    return {
      defaultAvatar,
      isShow: [],
    };
  },
  props: {
    remarkList: {
      type: Array,
      default: () => [],
    },
    total: {
      type: Number,
      default: 0,
    },
    remarkListLoading: {
      type: Boolean,
      default: false,
    },
  },
  created() {
    this.remarkList.forEach((item, index) => {
      this.$set(this.isShow, index, false); // 初始化状态为隐藏
    });
  },
  methods: {
    // 查看其他用户信息
    otherUserInfo(user) {},
    // 时间格式判断
    padZero(num) {
      return num < 10 ? "0" + num : num;
    },
    // 时间格式化
    formatDate(timeArray) {
      if (!timeArray) return "暂无时间";
      let year = timeArray[0];
      let month = this.padZero(timeArray[1]);
      let day = this.padZero(timeArray[2]);
      let hour = this.padZero(timeArray[3]);
      let minute = this.padZero(timeArray[4]);
      let second = this.padZero(timeArray[5]) || "00";
      return `${year}-${month}-${day} ${hour}:${minute}:${second}`;
    },
    // 判断换行符是否多于4个（有五行及以上文本）
    isFourLines(text) {
      // 使用正则匹配换行符
      const lines = text.match(/<br\/>/g);
      // 判断文字的个数
      const words = text.replace(/<br\/>/g, "").length;
      return words > 320 || (lines ? lines.length >= 4 : false);
    },
    // 切换状态
    toggleShow(index) {
      // 用this.$set()方法更新数组中的元素，确保响应式
      this.$set(this.isShow, index, !this.isShow[index]);
    },
  },
};
</script>

<style lang="less" scoped>
.remark {
  padding: 20px 20px;
  .remark-content {
    .title {
      font-size: 19px;
      padding-bottom: 4px;
      border-bottom: 1px solid #ddd;
      span {
        font-size: 15px;
        color: #999;
      }
    }
    .remark-item {
      display: flex;
      margin-top: 20px;
      margin-left: 16px;
      .avatar {
        width: 44px;
        height: 44px;
        border-radius: 50%;
      }
      .item-info {
        margin-left: 10px;
        padding-bottom: 30px;
        width: 100%;
        border-bottom: 1px solid #ddd;
        .info-header {
          margin: 8px 0;
          display: flex;
          align-items: flex-end;
          .nickname {
            font-size: 17px;
            color: #777;
          }
          .create-time {
            margin-left: 8px;
            font-size: 14px;
            color: #aaa;
          }
        }
        .content {
          // 超过四行隐藏
          display: -webkit-box;
          -webkit-box-orient: vertical;
          -webkit-line-clamp: 4;
          overflow: hidden;
        }
      }
    }
  }
}
</style>